// File: LoginView.java

package view;

import client.Client;
import client.ClientView;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import model.Flight;
import model.Sale;

/**
 * Creates a view to delete user.
 *
 * @author Tobias Robinson
 */
public class PurchaseTicketView extends JDialog implements ActionListener {

    //input to pass to UCLoginUser
    private int quantity = 0;
    private String flightID = null;
    private String origin = null;
    private String destination = null;
    private static String user = null;
    private JList list = null;


    private double gst = 0.0;
    private double originTax = 0.0;
    private double destinationTax = 0.0;
    private double flightPrice = 0.0;
    private double ticketPrice = 0.0;
    private double subPrice = 0.0;
    private double finalPrice = 0.0;
    private double discount = 0.0;
    private ArrayList<String> items = null;

    private JComboBox comboBox = null;

    //components of the login window
    private JTextField quantityField = new JTextField(2);
    private JLabel quantityLabel = new JLabel("Enter amount: ");
    private JLabel idLabel = new JLabel("Select Flight ID:  ");
    private JButton purchaseButton = new JButton("Purchase");

    /**
     * Creates a view to login user.
     *
     * @param parent tells view to run over main window
     */
    public PurchaseTicketView(JFrame parent) throws SQLException {
        super(parent, "Purchase Ticket", true);
        setResizable(true);

        //content pane for login window
        JPanel purchasePane = new JPanel();
        setContentPane(purchasePane);

        //user GridBag layout
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        purchasePane.setLayout(gb);
        purchasePane.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));

        // create a combo box
        items = Flight.getFlights();
        Object itemList[] = items.toArray();
        comboBox = new JComboBox(itemList);
        comboBox.setEditable(false);
        // create a list with the same data model
        list = new JList(comboBox.getModel());

        // place the id label
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(10, 0, 10, 0);
        c.anchor = GridBagConstraints.WEST;
        gb.setConstraints(idLabel, c);
        purchasePane.add(idLabel);

        //add combo box to panel
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 0, 5, 10);
        c.anchor = GridBagConstraints.EAST;
        gb.setConstraints(comboBox, c);
        purchasePane.add(comboBox);

        // place the quantity label
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(10, 0, 10, 0);
        c.anchor = GridBagConstraints.WEST;
        gb.setConstraints(quantityLabel, c);
        purchasePane.add(quantityLabel);

        // place the quantity field for the date
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 0, 5, 10);
        c.anchor = GridBagConstraints.EAST;
        gb.setConstraints(quantityField, c);
        purchasePane.add(quantityField);

        // place the login button
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 10, 5, 10);
        c.anchor = GridBagConstraints.CENTER;
        gb.setConstraints(purchaseButton, c);
        purchasePane.add(purchaseButton);

        // end of layout

        // Register password field and OK button with action event handler.
        // An action event is generated when the return key is pressed while
        // the cursor is in the password field or when the OK button is pressed.
        purchaseButton.addActionListener(this);
    }

    /**
     * When user click button or presses enter processes information
     *
     * @param e event type
     */
    public void actionPerformed(ActionEvent e) {
            flightID = comboBox.getSelectedItem().toString();
            quantity = Integer.parseInt(quantityField.getText());
            user = Client.getUser();
            
            try {
                origin = Flight.getOrigin(flightID);
                destination = Flight.getDestination(flightID);
                originTax = Sale.getAirportTax(origin);
                destinationTax = Sale.getAirportTax(destination);
                gst = Sale.getGST();
                flightPrice = Flight.getFlightPrice(flightID);
                if (quantity > Sale.getPromoMin()) {
                    discount = Sale.getDiscount();
                }
            } catch (SQLException ex) {
                Logger.getLogger(PurchaseTicketView.class.getName()).log(Level.SEVERE, null, ex);
            }

            subPrice = (flightPrice * gst);
            ticketPrice = ((flightPrice + subPrice) + (flightPrice * originTax) + (flightPrice * destinationTax) * (1 - discount));
            finalPrice = (ticketPrice * quantity);

            ClientView.printMessage(user + " purchased " + quantity + " tickets on Flight: " + flightID + "\n");
            ClientView.printMessage(origin + " Airport tax: " + originTax + "\n");
            ClientView.printMessage(destination + " Airport tax: " + destinationTax + "\n");
            ClientView.printMessage("GST = " + gst + "\n");
            ClientView.printMessage("Discount = " + discount + "\n");
            ClientView.printMessage("Price Per Ticket including GST and Promotion: $" + ticketPrice + "\n");
            ClientView.printMessage("Total Price: $ " + finalPrice + "\n");
            
            while (quantity > 0) {            
                Sale.purchaseTicket(flightID);            
                quantity --;
            }
            
            dispose();
    }
}
